﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Data.OleDb;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;

namespace YArchitech.Public.ExcelToView
{
	internal class ExcelLoader_OleDB : ExcelLoaderBase
	{
		private ExcelLoader_OleDB()
		{
		}

		internal static ExcelLoader_OleDB GetExcelLoader()
		{
			if (ExcelLoader_OleDB.singleTag == null)
			{
				ExcelLoader_OleDB.singleTag = new ExcelLoader_OleDB();
			}
			return ExcelLoader_OleDB.singleTag;
		}

		protected override bool LoadExcel(List<int> datas, int wantSheetIndex = -1, string wantSheetName = null, string inputPath = null)
		{
			if (-1 == wantSheetIndex && wantSheetName == null)
			{
				return false;
			}
			if (base.ExcelPath == null)
			{
				return false;
			}
			if (this.SearchValues.Keys.Contains(wantSheetIndex))
			{
				return false;
			}
			DataSet dataSet = new DataSet();
			string text = this.makeLinkString();
			OleDbConnection oleDbConnection = null;
			if (text == null)
			{
				return false;
			}
			try
			{
				oleDbConnection = new OleDbConnection(text);
				oleDbConnection.Open();
				DataTable oleDbSchemaTable = oleDbConnection.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, new object[]
				{
					null,
					null,
					null,
					StaticResources.Str_TABLE
				});
				string text2 = this.findSheetName(wantSheetIndex, oleDbSchemaTable, wantSheetName);
				if (text2 == null)
				{
					return false;
				}
				ExcelLoader_OleDB.fillDataSet(dataSet, oleDbConnection, text2);
				if (0 >= dataSet.Tables.Count)
				{
					return false;
				}
				DataTable nowExcelDataTable = dataSet.Tables[0];
				this.makeValues(wantSheetIndex, nowExcelDataTable, wantSheetName);
			}
			catch (Exception)
			{
				return false;
			}
			finally
			{
				if (oleDbConnection != null && oleDbConnection.State == ConnectionState.Open)
				{
					oleDbConnection.Close();
				}
			}
			return true;
		}

		private void makeValues(int wantSheetIndex, DataTable nowExcelDataTable, string wantSheetName = null)
		{
			List<List<string>> list = new List<List<string>>();
			int num = -1;
			for (int i = 0; i < nowExcelDataTable.Rows.Count; i++)
			{
				list.Add(new List<string>());
				num++;
				for (int j = 0; j < StaticResources.ColumnIndexInSql.Length; j++)
				{
					int columnIndex = StaticResources.ColumnIndexInSql[j];
					if (StaticResources.ColumnName.Contains(nowExcelDataTable.Rows[i][columnIndex].ToString()))
					{
						list.RemoveAt(num);
						num--;
						break;
					}
					if (string.IsNullOrEmpty(nowExcelDataTable.Rows[i][columnIndex].ToString()) || string.IsNullOrEmpty(nowExcelDataTable.Rows[i][columnIndex].ToString()))
					{
						list.RemoveAt(num);
						num--;
						break;
					}
					list[num].Add(nowExcelDataTable.Rows[i][columnIndex].ToString());
				}
			}
			if (wantSheetName == null)
			{
				this.SearchValues.Add(wantSheetIndex, list);
				return;
			}
			this.SearchValues.Add(-(this.SearchValues.Count + 1), list);
		}

		private static void fillDataSet(DataSet nowDataSet, OleDbConnection nowDnC, string tableName)
		{
			using (OleDbCommand oleDbCommand = nowDnC.CreateCommand())
			{
				oleDbCommand.CommandText = StaticResources.SqlStr_Select(tableName);
				OleDbDataAdapter oleDbDataAdapter = new OleDbDataAdapter(oleDbCommand);
				oleDbDataAdapter.Fill(nowDataSet, tableName);
			}
		}

		private string makeLinkString()
		{
			string text = Path.GetExtension(base.ExcelPath);
			Path.GetFileName(base.ExcelPath);
			text = text.ToLower();
			if (!StaticResources.ExcelExtend.Contains(text))
			{
				return null;
			}
			int num = StaticResources.ExcelExtend.ToList<string>().IndexOf(text);
			return StaticResources.LinkString[num][0] + base.ExcelPath + StaticResources.LinkString[num][1];
		}

		private string findSheetName(int wantSheetIndex, DataTable sechemaTable, string wantName = null)
		{
			int num = 0;
			string result = null;
			if (wantName == null)
			{
				IEnumerator enumerator = sechemaTable.Rows.GetEnumerator();
				{
					while (enumerator.MoveNext())
					{
						object obj = enumerator.Current;
						DataRow dataRow = (DataRow)obj;
						if (this.sheetNameCheck(dataRow[StaticResources.Str_TABLENAME].ToString()))
						{
							if (num == wantSheetIndex)
							{
								result = dataRow[StaticResources.Str_TABLENAME].ToString();
							}
							num++;
						}
					}
					return result;
				}
			}
			foreach (object obj2 in sechemaTable.Rows)
			{
				DataRow dataRow2 = (DataRow)obj2;
				if (this.sheetNameCheck(dataRow2[StaticResources.Str_TABLENAME].ToString()))
				{
					if (Regex.IsMatch(dataRow2[StaticResources.Str_TABLENAME].ToString(), wantName))
					{
						result = dataRow2[StaticResources.Str_TABLENAME].ToString();
					}
					num++;
				}
			}
			return result;
		}

		private bool sheetNameCheck(string input)
		{
			return !input.Contains("$") || input.Replace("'", "").EndsWith("$");
		}

		private static ExcelLoader_OleDB singleTag;
	}
}
